<?php
use entities\IM_Model;
use entities\IM_Model_picture;
use entities\IM_Model_stats;

class IM_C_Pub_Models extends IM_C_Base {
	public function start(){
		parent::start();
		parent::showNavi();
		parent::showBottomLeftNavi();
		
		$this->registry->template->show('Public-Models.php');
	}
	
	public function getMainContent(){
		$arr_out = array();
		$s_category = $_GET['category'];
		switch ($s_category){
			case 'main':
				$s_category = 'mainBoard';
				break;
			case 'intown':
				$s_category = 'inTown';
				break;
			case 'dev':
				$s_category = 'development';
				break;
			default:
				$s_category = 'mainBoard';
				break;
		}
		
		$s_gender = $_GET['gender'];
		
		if($s_gender == 'men'){
			$s_gender = 'male';
		}else{
			$s_gender = 'female';
		}
		$s_limitation = $_GET['limitation'];
		
		if(!isset($s_limitation) || $s_limitation == null || $s_limitation == '' || $s_limitation == 1){
			if($s_limitation == ''){
				$i_limitation = null;
			}else{
				$i_limitation = 0;	
			}
			
		}else{
			$s_limitation = (int)$s_limitation  -1;
			$i_limitation  = (int)$s_limitation * 18; 
		}
		
		$arr_models = $this->o_em->getRepository('entities\IM_Model')->getModels($s_gender, $s_category, $i_limitation);
		if(!empty($arr_models)){
			foreach($arr_models as $o_model){
				$i_modelId = $o_model->getId();
				//$o_model_coverPhoto = $this->o_em->find('entities\IM_Model_picture', $o_model->getCoverPhotoId());
				$o_model_coverPhoto = $this->o_em->getRepository('entities\IM_Model_picture')->findOneBy(array(
					'id_mod' => $i_modelId, 
					'id'=> $o_model->getCoverPhotoId()
				));
				
				if(!isset($o_model_coverPhoto) || $o_model_coverPhoto == null || empty($o_model_coverPhoto)){
					$arr_model_coverPhoto = $this->o_em->getRepository('entities\IM_Model_picture')->getModelPictureByModelId($i_modelId);
					//$this->o_em->findOneBy('entities\IM_Model_picture', (int)$o_model->getCoverPhotoId());
					if(!empty($arr_model_coverPhoto)){
						if(is_array($arr_model_coverPhoto)){
							$o_model_coverPhoto = $arr_model_coverPhoto[0];
							$i_coverPicId = $o_model_coverPhoto->getId();
							$s_relPath = fw_util_getFullUrl() . '/im_images_mod/thumbnails/' . $o_model_coverPhoto->getRelPath() . '/' . $o_model_coverPhoto->getFileName();
						}
					}else{
						$i_coverPicId = 'none';
						$s_relPath = 'n/a';
					}
					
				}else{
					$i_coverPicId = $o_model_coverPhoto->getId();
					$s_relPath = 'im_images_mod/thumbnails/' . $o_model_coverPhoto->getRelPath() . '/' . $o_model_coverPhoto->getFileName();
				}
				$i_modelId = EncryptionHandler::get()->doEncrypt($i_modelId);
				$i_coverPicId = EncryptionHandler::get()->doEncrypt($i_coverPicId) . $i_modelId;
				
				$s_name = $o_model->getName();
				$arr_modObject = array(
					strtolower($s_name),
					$i_modelId,
					//just to distract the potential hackers ?? hahah
					$i_coverPicId,
					$s_relPath,
					md5($i_modelId),
					md5($i_coverPicId)
				); 
				$arr_out[] = $arr_modObject;
				//array_push($arr_out, $arr_modObject);
				//array_push($arr_out, $arr_modObject);
				//array_push($arr_out, $arr_modObject);
				//array_push($arr_out, $arr_modObject);
				//array_push($arr_out, $arr_modObject);
			}
		}
		
		fw_util_outputJson($arr_out);
	}
	
	public function getCoverFlow(){
		//fw_util_outputJson($_GET);
		$arr_output = array();
		$i_modelId = EncryptionHandler::get()->doDecrypt($_GET['send']);
		$o_model = $this->o_em->find('entities\IM_Model', $i_modelId);
		$arr_pictures = $this->o_em->getRepository('entities\IM_Model_picture')->getModelPictureByModelId($i_modelId);
		$o_modelStats = $this->o_em->getRepository('entities\IM_Model_stats')->findOneBy(array('id_mod'=>$i_modelId));
		$arr_output['pics'] = array();
		if(!empty($arr_pictures)){
			
			foreach($arr_pictures as $o_picture){
				if($o_model->getCoverPhotoId() != $o_picture->getId()){
					$s_coverFlowPic = 'im_images_mod/thumbnails/' . $o_picture->getRelPath() . '/' . $o_picture->getFileName();
					if(file_exists($s_coverFlowPic)){
						$arr_output['pics'][] = $s_coverFlowPic;
					}else{
						$this->o_em->remove($o_picture);
					}
				}
			}
			$this->o_em->flush();
		}
		$arr_output['stats'] = array(
			$o_model->getName(),
			$o_modelStats->getHeight(),
			$o_modelStats->getChest(),
			$o_modelStats->getWaist(),
			$o_modelStats->getHips(),
			$o_modelStats->getShoe(),
			$o_modelStats->getHair(),
			$o_modelStats->getEyes()
		);
		fw_util_outputJson($arr_output);
	}
	
	public function CoverFlow(){
		if((isset($_GET['ledom'])) && (trim($_GET['ledom']) != ''  || trim($_GET['ledom'] != null))){
			parent::start();
			parent::showNavi();
			parent::showBottomLeftNavi();
			
			$arr_output = array();
			$i_modelId = EncryptionHandler::get()->doDecrypt($_GET['ledom']);
			$o_model = $this->o_em->find('entities\IM_Model', $i_modelId);
			$arr_pictures = $this->o_em->getRepository('entities\IM_Model_picture')->getModelPictureByModelId($i_modelId);
			if(!empty($arr_pictures)){
				foreach($arr_pictures as $o_picture){
					$arr_output[] = fw_util_getFullUrl().'/im_images_mod/thumbnails/' . $o_picture->getRelPath() . '/' . $o_picture->getFileName();
				}
			}
			
			$this->registry->template->arr_modelPics = $arr_output;
			$this->registry->template->show('Public-Models-Coverflow.php');
		}else{
			fw_util_redirect('IM_ModelsMain');
		}
	}
}